package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.EmployeeNotice;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.domain.Notice;
import cn.wolfcode.service.IEmployeeNoticeService;
import cn.wolfcode.service.INoticeService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private INoticeService noticeService;
    @Autowired
    private IEmployeeNoticeService employeeNoticeService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        //查询所有公告板信息
        PageInfo<Notice> pageInfo = noticeService.query(qo);
        //获取当前登录对象id
        Long id = UserContext.getCurrentUser().getId();
        //插入单前员工id获取对应的公告信息
        this.selectNoticeByUserId(qo,id);
        //查询该用户的已读信息
        List<EmployeeNotice> employeeNotices = employeeNoticeService.selectStatus(id);
        //把当前用户返回到页面
        Employee user = UserContext.getCurrentUser();
        //查询公告等级
        model.addAttribute("user",user);
        model.addAttribute("employeeNotices",employeeNotices);
        model.addAttribute("pageInfo", pageInfo);
        return "notice/list";
    }


    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           noticeService.delete(id);
        }
        return "redirect:/notice/list";
    }


    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Notice notice) {
        if (notice.getId() == null) {
            noticeService.save(notice);
        } else {
            noticeService.update(notice);
        }
        return "redirect:/notice/list";
    }

    @RequestMapping("/publish")
    public String publish(Long id){
        if (id!=null){
            noticeService.updatePublishStatus(id);
        }
        return "redirect:/notice/list";
    }

    @RequestMapping("/show")
    public String show() {

        return "notice/show";
    }

    @RequestMapping("/showNotice")
    @ResponseBody
    public JsonResult showNotice(){
        //先判断是管理员还是普通员工
        Employee employee = UserContext.getCurrentUser();
        if (employee.isAdmin()){
            List<Notice> notices = noticeService.adminNoticeById(employee.getId());
            UserContext.setCurrentNotice(notices);
        }else {
            List<Notice> notices = noticeService.userNoticeById(employee.getId());
            UserContext.setCurrentNotice(notices);
        }
        return new JsonResult();
    }

    public void selectNoticeByUserId(QueryObject qo,Long id){
        PageInfo<Notice> pageInfo = noticeService.query(qo);
        //获取公告板中的id集合
        List<Long> ids = new ArrayList<>();
        for (Notice notice:pageInfo.getList()){
            ids.add(notice.getId());
        }
        //查询当前员工的中间表关联
        List<EmployeeNotice>  employeeNotices = employeeNoticeService.selectStatus(id);
        List<Long> notices = new ArrayList<>();
        for (EmployeeNotice employeeNotice:employeeNotices){
            notices.add(employeeNotice.getNotice());
        }
        //遍历公告板id
        for (int i=0;i <ids.size();i++){
            //如果employeeNotices为空证明是第一次登陆,直接插入所有数据
            if (employeeNotices==null){
                EmployeeNotice employeeNotice1 = new EmployeeNotice();
                employeeNotice1.setEmployee_id(id);
                employeeNotice1.setNotice(ids.get(i));
                employeeNotice1.setRead_status(false);
                employeeNoticeService.save(employeeNotice1);
            }else {
                ids.removeAll(notices);
            }
        }
        //删除完后再添加新的关联到中间表
        for (Long addId:ids){
            EmployeeNotice employeeNotice1 = new EmployeeNotice();
            employeeNotice1.setEmployee_id(id);
            employeeNotice1.setNotice(addId);
            employeeNotice1.setRead_status(false);
            employeeNoticeService.save(employeeNotice1);
        }
    }
}
